//
//  XDVideoHeaderView.m
//  XDEducation
//
//  Created by apple on 2017/4/21.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDVideoHeaderView.h"

@interface XDVideoHeaderView ()

/** 免费 */
@property(nonatomic,strong) UIButton *freeBtn;

/** 付费 */
@property(nonatomic,strong) UIButton *payBtn;
@end

@implementation XDVideoHeaderView



-(instancetype)init{

    if (self = [super init]) {
        
        _freeBtn = [[UIButton alloc]init];
        [_freeBtn setTitle:@"免费" forState:UIControlStateNormal];
        [_freeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _freeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_freeBtn setBackgroundImage:[UIImage imageNamed:@"video_btn_category_default"] forState:UIControlStateNormal];
        [_freeBtn setBackgroundImage:[UIImage imageNamed:@"video_btn_category_selected"] forState:UIControlStateSelected];
        [self addSubview:_freeBtn];
        
        
        _payBtn = [[UIButton alloc]init];
        [_payBtn setTitle:@"付费" forState:UIControlStateNormal];
        [_payBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _payBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_payBtn setBackgroundImage:[UIImage imageNamed:@"video_btn_category_default"] forState:UIControlStateNormal];
        [_payBtn setBackgroundImage:[UIImage imageNamed:@"video_btn_category_selected"] forState:UIControlStateSelected];
        [self addSubview:_payBtn];
        
        _freeBtn.selected = YES;
        
        [_freeBtn addTarget:self action:@selector(freeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_payBtn addTarget:self action:@selector(payBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat buttonW = 50;
    CGFloat buttonH = 25;
//    CGFloat padding = 15;
    
    [_payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-15);
        make.centerY.equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(buttonW, buttonH));
    }];
    
    
    [_freeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_payBtn.mas_left).offset(-10);
        make.centerY.equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(buttonW, buttonH));
    }];
}


-(void)freeBtnClicked:(UIButton *)button{

    if (_payBtn.selected) {
        _payBtn.selected = !_payBtn.selected;
    }
    
    if (!button.selected) {
        button.selected = !button.selected;
        
        if (self.freeBlock) {
            self.freeBlock();
        }
    }
    
   
}


-(void)payBtnClicked:(UIButton *)button{
    
    if (_freeBtn.selected) {
        _freeBtn.selected = !_freeBtn.selected;
    }
    
    if (!button.selected) {
        button.selected = !button.selected;
        
        if (self.payBlock) {
            self.payBlock();
        }
    }
    
    
    
}
@end
